package com.zz.comframe;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.zz.comframe.properties.PropertiesPool;

public class DBHelp {
	static {
		try {
//			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			//Class.forName("org.hsqldb.jdbcDriver");
			String classname = PropertiesPool.getEncryptpro("class-derby");
			Class.forName(classname);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
//	private String connStr = "jdbc:sqlserver://localhost:1433;DatabaseName=student";
	//private String connStr = "jdbc:hsqldb:res:/test";
	//private String connStr = "jdbc:derby://localhost:1527/db2012";
	private String connStr = PropertiesPool.getEncryptpro("connstring-derby");
	//private String userName = "sa";
	//private String userPass = "";
	//private String userName = "db2012";
	//private String userPass = "db2012";
	private String userName = PropertiesPool.getEncryptpro("dbun");
	private String userPass = PropertiesPool.getEncryptpro("pw");
	private Connection connection = null;

	public Connection getConnection() {
		try {
			Class.forName(PropertiesPool.getEncryptpro("class-derby"));
			connection = DriverManager.getConnection(connStr, userName,
					userPass);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return connection;
	}

	public void closeResource(Object obj) {
		try {
			if (Connection.class.isInstance(obj)) {
				((Connection) obj).close();
			}
			if (PreparedStatement.class.isInstance(obj)) {
				((PreparedStatement) obj).close();
			}

			if (ResultSet.class.isInstance(obj)) {
				((ResultSet) obj).close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void closeResource(Connection connection,
			PreparedStatement pStatement) {
		try {
			if (connection != null) {
				connection.close();
			}
			if (pStatement != null) {
				pStatement.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void closeResource(Connection connection,
			PreparedStatement pStatement, ResultSet resultSet) {
		try {
			if (connection != null) {
				connection.close();
			}
			if (pStatement != null) {
				pStatement.close();
			}
			if (resultSet != null) {
				resultSet.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
